<?php

define('PRODUCT_TTL', 0);
define('PRODUCT_PRICES_TTL', 86400);
define('LOOKUP_CACHE_TTL', 0);

class iSpringServices
{
    /**
     * @var iSpringRestClient
     */
    private static $restClient;

    /**
     * @var ICache
     */
    private static $cache;

    private static $storeId = null;

    public static function Init( $url, $login = '', $password = '' )
    {
        self::$restClient = new iSpringRestClient( $url );
        self::$restClient->SetAuthorizationData( $login, $password );
        self::$cache = new NullCache();
    }

    public static function SetCache( ICache $cache )
    {
        self::$cache = $cache;
    }

    public static function GetStoreId($host)
    {
        $storeId = null;

        $id = "shopping_cart/store/" . $host;
        $storeId = self::$cache->Get($id);

        if (!$storeId)
        {
            $storeId = self::$restClient->GetStoreId($host);
            if ($storeId)
            {
                self::$cache->Set($id, $storeId, LOOKUP_CACHE_TTL);
            }
        }

        return $storeId;
    }

    public static function GetCurrentStoreId()
    {
        if (is_null(self::$storeId)) {
            self::$storeId = self::GetStoreId(STORE_NAME);
        }
        return self::$storeId;
    }

    public static function GetProductInfo( $prodId )
    {
        $id = "products/product" . (is_null($prodId) ? '' : "_" . $prodId);
        $product = self::$cache->Get($id);
        if ( is_null($product) )
        {
            $product = self::$restClient->GetProductInfo( $prodId );
            if ( !empty($product) )
            {
                self::$cache->Set($id, $product, PRODUCT_TTL);
            }
        }
        return $product;
    }

    public static function GetProductsInfo( $prodId = null )
    {
        $id = "products/products" . (is_null($prodId) ? '' : "_prodId");
        $products = self::$cache->Get($id);
        if ( is_null($products) )
        {
            $products = self::$restClient->GetProductsInfo( $prodId );
            if ( !empty($products) )
            {
                self::$cache->Set($id, $products, PRODUCT_TTL);
            }
        }
        return $products;
    }

    /**
    *
    * @param string $priceType One of the PriceType value
    * @return array An array of PriceInfo objects
    */
    public static function GetProductsPrices( $priceType )
    {
        $prices = null;
        if ( empty( $coupons ) )
        {
            $id = "products/prices/" . $priceType;
            $prices = self::$cache->Get($id);
        }
        if ( is_null($prices) )
        {
            $prices = self::$restClient->GetProductsPrices( $priceType, self::GetCurrentStoreId() );
            if ( !empty($prices) )
            {
                // store in cache only if coupons not specified
                self::$cache->Set($id, $prices, PRODUCT_PRICES_TTL);
            }
        }

        return $prices;
    }

    /**
     *
     * @param string $product
     * @param string $priceType One of the PriceType value
     * @param array $coupons
     * @return array An array of PriceInfo objects
     */
    public static function GetProductPrices( $product, $priceType, $coupons = array() )
    {
        $prices = null;
        if ( empty( $coupons ) )
        {
            $id = "products/product_" . $product . "/prices/" . $priceType;
            $prices = self::$cache->Get($id);
        }
        if ( is_null($prices) )
        {
            $prices = self::$restClient->GetProductPrices( $product, $priceType, $coupons );
            if ( !empty($prices) && empty($coupons) )
            {
                // store in cache only if coupons not specified
                self::$cache->Set($id, $prices, PRODUCT_PRICES_TTL);
            }
        }

        return $prices;
    }

    public static function GetActualProducts()
    {
        $id = "products/latest_products";
        $products = self::$cache->Get($id);
        if ( is_null($products) )
        {
            $products = self::$restClient->GetProductsActual();
            if ( !empty($products) )
            {
                self::$cache->Set($id, $products, PRODUCT_TTL);
            }
        }
        return $products;
    }

    public static function SendMail( MailMessage $msg )
    {
        self::$restClient->SendMail( $msg );
    }

    public static function CreateLicense(
        $gate, $product, $license,  $version, $name,
        $email = null, $phone = null, $ip_address = null, $uid = null, $company = null, $web = null, $country = null,
        $quantity = null, $total = null, $order_id = null, $password = null, $upgrade = null
    )
    {
        return self::$restClient->CreateLicense(
            $gate, $product, $license,  $version, $name, $email, $phone,
            $ip_address, $uid, $company, $web, $country, $quantity,
            $total, $order_id, $password, $upgrade
        );
    }
}

?>